Math Functions in C:
Trigonometry, exponentiation, logarithms, rounding, and other mathematical operations are available in the Cs math library. These approaches are more suited for complex calculations than simple arithmetic programming. The header fileC Math Functions
There are various math methods.h header file. The commonly used functions of math.h header files are given below.Here are some examples of common math function categories in C:
NO | Function | Description |
---|---|---|
1 | ceil(number) | rounds up the given number. It returns the integer value which is greater than or equal to given number |
2 | floor(number) | rounds down the given number. It returns the integer value which is less than or equal to given number. |
3 | sqrt(number) | returns the square root of given number. |
4 | pow(base, exponent) | returns the power of given number. |
5 | abs(number) | returns the absolute value of given number. |
C Math Example
Let's see a simple example of math functions found in math.h header file.
#include
#include
int main(){
printf("n%f",ceil(3.6));
printf("n%f",ceil(3.3));
printf("n%f",floor(3.6));
printf("n%f",floor(3.2));
printf("n%f",sqrt(16));
printf("n%f",sqrt(7));
printf("n%f",pow(2,4));
printf("n%f",pow(3,3));
printf("n%d",abs(-12));
return 0;
}
Output
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12
Trigonometric Functions:
The trigonometric functions are also used in the math function. The sine, cosine, and tangent of an angle are calculated using the functions sin(), cos(), and sin().Other known mathematical functions besides trigonometric functions are:
- exp(): returns a number's exponential value (ex).
- log() calculates a number's natural logarithm (base e).
- log10() computes a number's common logarithm (base 10).
- pow() raises a number to a given power.
Rounding Functions:
Following are some rounding functions that used in math function:- ceil() rounds a number up to the nearest integer.
- floor () reduces a number to the nearest integer.
- round() returns the nearest integer to a floating-point number.
Other Mathematical Functions:
Following are some other mathematical functions that used in math function:- sqrt() calculates the square root of a number.
- fabs() returns the absolute value of a number.
- fmod() calculates the remainder of the division between two numbers.
Header:
If you want to utilize these arithmetic functions in your C application, include the
#include
#include
int main() {
double x = 2.0;
double result = sqrt(x);
printf("The square root of %.2f is %.2fn", x, result);
return 0;
}
Output
The square root of 2.00 is 1.41
Explanation:
In this example, the1. sqrt() - Square Root Function
sqrt() function determines an integer's square root.Example:
#include
#include
int main() {
double x = 25.0;
double result = sqrt(x);
printf("The square root of %.2f is %.2fn", x, result);
return 0;
}
Output
The square root of 25.00 is 5.00
2. pow() - Exponential Power Function
The pow() function raises a number to a given power.Example:
#include
#include
int main() {
double base = 2.0;
double exponent = 3.0;
double result = pow(base, exponent);
printf("%.2f raised to the power of %.2f is %.2fn", base, exponent, result);
return 0;
}
Output
2.00 raised to the power of 3.00 is 8.00
3. sin(), cos(), and tan() - Trigonometric Function
These formulas calculate an angle's sine,cosine, and tangent in radians.Example:
#include
#include
int main() {
double angle = 1.0; // in radians
double sin_val = sin(angle);
double cos_val = cos(angle);
double tan_val = tan(angle);
printf("For angle %.2f:n", angle);
printf("Sine: %.2fn", sin_val);
printf("Cosine: %.2fn", cos_val);
printf("Tangent: %.2fn", tan_val);
return 0;
}
Output
For angle 1.00:
Sine: 0.84
Cosine: 0.54
Tangent: 1.56
4. exp() - Exponential Function
The exp() function determines the number's exponential value (ex).Example:
#include
#include
int main() {
double x = 2.0;
double result = exp(x);
printf("e raised to the power of %.2f is %.2fn", x, result);
return 0;
}
Output
e raised to the power of 2.00 is 7.39
5. log() and log10() - Logarithmic Functions
The log() function calculates the natural logarithm (base e) of a number, while log10() calculates the common logarithm (base 10).Example:
#include
#include
int main() {
double x = 100.0;
double natural_log = log(x);
double common_log = log10(x);
printf("Natural logarithm of %.2f is %.2fn", x, natural_log);
printf("Common logarithm of %.2f is %.2fn", x, common_log);
return 0;
}
Output
Natural logarithm of 100.00 is 4.61
Common logarithm of 100.00 is 2.00
6. ceil() and floor() - Rounding Functions
The ceil() function rounds a number up to the nearest integer, while floor() rounds it down.Example:
#include
#include
int main() {
double x = 3.7;
double y = -2.3;
printf("Ceil of %.2f is %.2fn", x, ceil(x));
printf("Floor of %.2f is %.2fn", x, floor(x));
printf("Ceil of %.2f is %.2fn", y, ceil(y));
printf("Floor of %.2f is %.2fn", y, floor(y));
return 0;
}
Output
Ceil of 3.70 is 4.00
Floor of 3.70 is 3.00
Ceil of -2.30 is -2.00
Floor of -2.30 is -3.00
7. fabs() - Absolute Value Function
The fabs() function returns the absolute value of a given number, which is its distance from zero.Example:
#include
#include
int main() {
double x = -5.0;
double result = fabs(x);
printf("The absolute value of %.2f is %.2fn", x, result);
return 0;
}
Output
The absolute value of -5.00 is 5.00
8. fmod() - Remainder Calculation Function
The remaining value of a two-number division is calculated using the fmod() function.Example:
#include
#include
int main() {
double dividend = 10.0;
double divisor = 3.0;
double remainder = fmod(dividend, divisor);
printf("The remainder of %.2f divided by %.2f is %.2fn", dividend, divisor, remainder);
return 0;
}
Output
The remainder of 10.00 divided by 3.00 is 1.00
9. sinh() and cosh() - Hyperbolic Sine and Cosine Functions
A given integer hyperbolic sine or cosine can be calculated using the sinh() and cosh() functions.Example:
#include
#include
int main() {
double x = 2.0;
double sinh_val = sinh(x);
double cosh_val = cosh(x);
printf("Hyperbolic sine of %.2f is %.2fn", x, sinh_val);
printf("Hyperbolic cosine of %.2f is %.2fn", x, cosh_val);
return 0;
}
Output
Hyperbolic sine of 2.00 is 3.63
Hyperbolic cosine of 2.00 is 3.76
10. atan2() - Arc Tangent Function (2 Arguments)
Considering both of the arguments' signs, the atan2() function calculates the arctangent of the quotient of its two inputs.Example:
#include
#include
int main() {
double x = 1.0;
double y = 1.0;
double result = atan2(y, x);
printf("The arctangent of y=%.2f and x=%.2f is %.2f radiansn", y, x, result);
return 0;
}
Output
The arctangent of y=1.00 and x=1.00 is 0.79 radians
11. round() - Rounding to Nearest Integer
When given a floating-point number, the round() function returns the closest integer.Example:
#include
#include
int main() {
double x = 3.6;
double y = 3.3;
printf("Rounded value of %.2f is %.2fn", x, round(x));
printf("Rounded value of %.2f is %.2fn", y, round(y));
return 0;
}
Output
Rounded value of 3.60 is 4.00
Rounded value of 3.30 is 3.00